<template>
  <div class="channel-edit">
    <!-- 当前登陆用户已经订阅的频道 -->
    <div class="channel">
      <van-cell
        title="我的频道"
        :border="false"
      >
        <van-button
          size="mini"
          type="info"
          @click="editing=!editing"
        >{{editing?'取消':'编辑'}}</van-button>
      </van-cell>
      <van-grid>
        <van-grid-item
          v-for="(channel, idx) in channels"
          :key="channel.id"
          @click="hClickMyChannel(idx)"
          :class="{red:idx===curIndex}"
        >
          <span>{{channel.name}}</span>
          <van-icon
            v-show="editing && idx !=0"
            name="cross"
            class="btn"
          ></van-icon>
        </van-grid-item>
      </van-grid>
    </div>
    <!-- 当前登陆用户没有订阅的频道 -->
    <div class="channel">
      <van-cell
        title="可选频道"
        :border="false"
      ></van-cell>
      <van-grid>
        <van-grid-item
          v-for="channel in recommendChannels"
          :key="channel.id"
          @click="hClickRecommandChannel(channel)"
        >
          <span>{{channel.name}}</span>
        </van-grid-item>
      </van-grid>
    </div>
  </div>
</template>

<script>
import { getAllChannels, addChannel, delChannel } from '@/api/channel.js'
export default {
  name: 'ChannelEdit',
  props: ['channels', 'curIndex'],
  data () {
    return {
      allChannels: [],
      editing: false
    }
  },
  created () {
    this.loadAllChannels()
  },
  methods: {
    // 新增功能
    async hClickRecommandChannel (curChannel) {
      try {
        const channels = [...this.channels, curChannel].map((item, idx) => {
          return {
            id: item.id,
            seq: idx
          }
        }).filter(item => item.id !== 0)
        await addChannel(channels)
        this.channels.push(curChannel)
        this.$toast.success('订阅频道成功')
      } catch (err) {
        this.$toast.fail('订阅失败')
      }
    },
    // 判断如果是编辑状态，删除，如果是取消状态，关闭弹层跳转
    hClickMyChannel (idx) {
      if (this.editing) {
        this.doDeleteChannel(idx)
      } else {
        this.$emit('change-channel', idx)
      }
    },
    // 删除功能
    async doDeleteChannel (idx) {
      const id = this.channels[idx].id
      try {
        const res = await delChannel(id)
        console.log(res)
        this.$toast.success('删除成功')
        if (idx < this.curIndex) {
          this.$emit('fixed-active-index')
        }
        // 从新加载页面
        this.channels.splice(idx, 1)
      } catch (err) {
        this.$toast.fail('删除失败')
      }
    },
    async loadAllChannels () {
      const res = await getAllChannels()
      this.allChannels = res.data.data.channels
    }
  },
  computed: {
    recommendChannels () {
      const arr = this.allChannels.filter(channel => {
        const idx = this.channels.findIndex(item => item.id === channel.id)
        if (idx === -1) {
          return true
        }
      })
      return arr
    }
  }
}
</script>

<style lang="less" scoped>
.channel {
  padding: 15px;
  font-size: 14px;
}
.btn {
  position: absolute;
  top: 0;
  right: 0;
  font-size: 24px;
}
.red {
  color: red;
  font-weight: bold;
}
</style>
